﻿using NiklasKarl.WarpCam;
using Nokia.Graphics.Imaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage.Streams;
using System.Runtime.InteropServices.WindowsRuntime;
using NiklasKarl.WarpCam.Native;
using NiklasKarl.WarpCam.Effects;
using System.Threading;

namespace NiklasKarl.WarpCam.Sources
{
    /*public class SingleViewfinderSource : IViewfinderSource
    {
        private Semaphore m_semaphore;

        private ICameraEffect m_effect;

        private IImageProvider m_cameraSource;
        private ColorImageSource m_blackSource;

        private BufferWrapper m_prevBufferWrapper;
        private Size m_prevResolution;

        private IBuffer m_destBuffer;
        private Bitmap m_destBitmap;

        public SingleViewfinderSource()
        {
            m_semaphore = new Semaphore(1, 1);
        }

        public IImageProvider CameraSource
        {
            get { return m_cameraSource; }
            set
            {
                Lock();
                m_cameraSource = value;
                ReleaseLock();
            }
        }

        public ICameraEffect Effect
        {
            get { return m_effect; }
            set { m_effect = value; }
        }

        public void Lock()
        {
            m_semaphore.WaitOne();
        }

        public void ReleaseLock()
        {
            m_semaphore.Release();
        }

        public void RenderBitmap(
            BufferWrapper bufferWrapper,
            Size resolution,
            Rotation rotation,
            FlipMode flip
                )
        {
            #if DEBUG
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            #endif

            if (!bufferWrapper.Equals(m_prevBufferWrapper) || resolution != m_prevResolution)
            {
                // Preparing
                int resolutionWidth = (int)resolution.Width;
                int resolutionHeight = (int)resolution.Height;

                // First create the unfiltered bitmap
                m_destBuffer = bufferWrapper.AsBuffer();
                m_destBitmap = new Bitmap(resolution, ColorMode.Bgra8888, (uint)resolutionWidth * 4, m_destBuffer);

                m_blackSource = new ColorImageSource(resolution, Windows.UI.Color.FromArgb(255, 0, 0, 0));

                m_prevBufferWrapper = bufferWrapper;
                m_prevResolution = resolution;
            }

            // Rendering
            Lock();

            if (m_cameraSource != null)
            {
                if (m_effect != null)
                {
                    m_effect.GetBitmapImage(m_cameraSource, m_destBitmap, rotation).Wait();
                }
                else
                {
                    m_cameraSource.GetBitmapAsync(m_destBitmap, OutputOption.PreserveAspectRatio).AsTask().Wait();
                }
            }
            else
            {
                m_blackSource.GetBitmapAsync(m_destBitmap, OutputOption.Stretch).AsTask().Wait();
            }

            ReleaseLock();

            #if DEBUG
            watch.Stop();
            System.Diagnostics.Debug.WriteLine("Rendering of SingleViewfinder took {0} ms.", watch.ElapsedMilliseconds);
            #endif
        }
    }*/
}
